<!-- 游戏默认配置 -->
<template>
  <SearchTable ref="SearchTableRef" :query="data.query" :list="data.list" :total="data.total" @changeTh="data.dialogVisible = true"
    @changeSize="changeSize" @changePage="changePage" @search="getSearchTypeList" @reset="resetDetails">
    <template #other>
      <el-tabs v-model="data.gameType" type="card" class="demo-tabs" @tab-change="handleClick"
        style="user-select: none;">
        <el-tab-pane v-for="(item, index) in data.tabDataList" :key="index" :label="item.label" :name="item.value" />
      </el-tabs>
    </template>
    <template #form>
      <span :class="!data.gameType ? 'transparency' : ''">
        <Operator ref="OperatorRef" @query="queryItem" />
        <GameItem ref="GameItemRef" @query="queryGame" :thirdShow="data.gameType!=4?false:true"/>
        <!-- <el-form-item label="" v-if="data.gameType==-1?true:false">
            <el-button type="warning" v-permiss="'/system/destroyManager'" size="small"
              @click="">
              GameUrl不一致
            </el-button>
          </el-form-item> -->
      </span>
      <el-form-item label="ID：" v-if="!data.gameType ? true : false">
        <el-input class="width240" type="text" v-model="data.globalQuery.id" placeholder="请输入数据ID" clearable />
      </el-form-item>
      <el-form-item label="备注：" v-if="!data.gameType ? true : false">
        <el-input class="width240" type="text" v-model="data.globalQuery.remark" placeholder="请输入备注" clearable />
      </el-form-item>
    </template>
    <template #button>
      <el-button type="warning" v-if="(data.gameType != -1 && data.gameType != 4) ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="initialConfiguration">
        初始化{{ selectToLabel(data.tabDataList, data.gameType) }}
      </el-button>
      <el-button type="success" v-if="data.gameType < 1 ? true : false" v-permiss="'/system/destroyManager'"
        @click="addDefaultConfig">
        新增默认配置
      </el-button>
      <el-button type="danger" v-if="data.gameType != 4 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="getCommercialList(-1), data.isShowCommercial = true">
        同步商户配置
      </el-button>
      <el-button type="warning" v-if="data.gameType == 2 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="initializeThousandSet">
        初始化千倍合约
      </el-button>
      <el-button type="danger" v-if="data.gameType == 2 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="data.isShowThousandCommercial = true">
        同步商户千倍合约
      </el-button>
      <el-button type="primary" v-show="data.gameType == -1 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="data.isShowBatchAgent = true">
        批量编辑商户链接
      </el-button>
      <el-button type="danger" v-show="data.gameType == -1 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="data.isShowBatch = true">
        批量编辑默认表链接
      </el-button>
      <el-button type="danger" v-show="data.gameType == -1 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="data.isShowLoad = true">
        重载配置表
      </el-button>
      <el-button type="warning" v-show="data.gameType == -1 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="data.isShowInitialize = true">
        初始化游戏表
      </el-button>
      <el-button type="warning" v-show="data.gameType == -1 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="data.isShowScale = true">
        最小倍数押注初始化
      </el-button>
      <el-button type="success" v-show="data.gameType == 4 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="data.isShowThirdDetails = true">
        新增第三方平台ID
      </el-button>
      <el-button type="warning" v-show="data.gameType == -1 ? true : false" v-permiss="'/system/destroyManager'"
        size="small" @click="data.isShowThirdPlatform = true">
        第三方平台ID维护
      </el-button>
      <el-button type="danger" v-show="data.gameType == 4 ? true : false" :disabled="data.list.length?false:true" v-permiss="'/system/destroyManager'"
        size="small" @click="getThirdGameIdList(true)">
        批量平替开关
      </el-button>
      <span class="colorRed" v-show="data.gameType == 4 ? true : false">
        此功能表在初始化时，请确认功能开关（state）字段参数与当前后台页面展示保持一致！数据确认无误后才可重载
      </span>
    </template>
    <template #table>
      <template v-for="(item, index) in data.fields" :key="index">
        <el-table-column v-if="item.visible" :prop="item.prop" :label="item.label" :width="item.width"
          show-overflow-tooltip>
          <template #header>
            <el-tooltip v-if="item.tips" effect="dark" placement="top">
              <template #content> {{ item.tips }} </template>
              <el-icon class="ques-tips">
                <QuestionFilled />
              </el-icon>
            </el-tooltip>
            {{ item.label }}
          </template>
          <template v-if="item.prop == 'game_name'" #default="scope">
            <GameHeaderInfo
              :class="selectToLabel(data.tabDataList, data.gameType, 'gameType', 'value') > 0 ? 'paddingLeft42' : ''"
              :game_id="scope.row.game_id" />
          </template>
          <template v-if="item.prop == 'game_type'" #default="scope">
            <el-tag>{{ selectToLabel(GAMETYPE, scope.row.game_type) }}</el-tag>
          </template>
          <template v-if="item.prop == 'status'" #default="scope">
            <el-tag :type="(scope.row.status && scope.row.status!=2 ) ? 'success' : 'danger'">{{ selectToLabel(data.gameType == 4?data.statusList:DEMOPLAY, scope.row.status)
              }}</el-tag>
          </template>
          <template v-if="item.prop == 'platform'" #default="scope">
            <el-tag>{{ selectToLabel(GameBrand, scope.row.platform) }}</el-tag>
          </template>
          <template v-if="item.prop == 'state'" #default="scope">
            <el-tag :type="scope.row.state ? 'success' : 'danger'">{{ selectToLabel(DEMOPLAY, scope.row.state)
              }}</el-tag>
          </template>
        </el-table-column>
      </template>
      <el-table-column fixed="right" label="操作" :width="data.gameType==4?'320':'150'" align="center">
        <template #default="scope">
          <el-button v-if="data.gameType==4?false:true" type="primary" v-permiss="'/system/destroyManager'" size="small"
            @click="checkDetails(scope.row, 'check')">
            查 看
          </el-button>
          <el-button type="warning" v-permiss="'/system/destroyManager'" size="small"
            @click="checkDetails(scope.row, 'edit')">
            编 辑
          </el-button>
          <!-- <el-button v-if="data.gameType == 1 ? true : false" type="danger" v-permiss="'/system/destroyManager'"
            size="small" @click="controlDetails(scope.row)">
            调控配置
          </el-button> -->
          <el-button v-if="data.gameType == 4 ? true : false" type="danger" v-permiss="'/system/destroyManager'" size="small"
            @click="deleteThird(scope.row)">
            删除
          </el-button>
          <el-button v-if="data.gameType == 4 ? true : false" :disabled="((!scope.row.status || scope.row.status==2) && scope.row.alternate_id)?false:true" type="success"
            v-permiss="'/system/destroyManager'" size="small"
            @click="maintenanceGame(scope.row,true)">
            开启游戏
          </el-button>
          <el-button v-if="data.gameType == 4 ? true : false" :disabled="((scope.row.status==1) && scope.row.alternate_id)?false:true" type="danger"
            v-permiss="'/system/destroyManager'" size="small"
            @click="maintenanceGame(scope.row,false)">
            维护游戏
          </el-button>
        </template>
      </el-table-column>
    </template>
  </SearchTable>

  <!-- 变更表头弹框 -->
  <tableHeader :dialogVisible="data.dialogVisible" :list="data.fields" @cancel="data.dialogVisible = false"
    @resetData="resetData"></tableHeader>

  <!-- 默认配置 -->
  <Game :show="data.isShowGame" :isDisableUser="data.isDisableUser" :checkedItem="data.checkedGameItem"
    @onHideDetails="onHideDetails" />
  <Config :show="data.isShowDetails" :isDisableUser="data.isDisableUser" :showDefault="true"
    :checkedItem="data.checkedItem" @onHideDetails="onHideDetails" />

  <!-- 默认全局表配置 -->
  <Global :show="data.isShowGlobal" :isDisableUser="data.isDisableUser" :checkedItem="data.checkedGlobalItem"
    @onHideDetails="onHideDetails" />

  <!-- 调控配置 -->
  <RtpControl :show="data.isShowControl" :checkedItem="data.checkedItem" @onHideDetails="onHideDetails" />

  <!-- 重载表 -->
  <el-dialog v-model="data.isShowLoad" title="重载表" :close-on-click-modal="false" :close-on-press-escape="false"
    width="50%" top="25vh" @close="onHideDetails(false)">
    <form-item title="表名" :pass="true" value="overloadTable">
      <el-select class="formItem240" v-model="data.overloadTable" placeholder="请选择表名" filterable>
        <el-option v-for="(item, index) in data.loadTable" :key="index"
          :label="'【' + item.table_name + '】' + item.table_comment" :value="item.table_name" />
      </el-select>
    </form-item>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" @click="overload">确 定</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 批量编辑商户 -->
  <el-dialog v-model="data.isShowBatchAgent" title="批量编辑商户链接" :close-on-click-modal="false"
    :close-on-press-escape="false" width="50%" top="20vh" @close="onHideDetails(false)">
    <BatchControl ref="BatchControlAgentRef" :brandShow="true" @query="gameLinkAgent">
      <template #footer>
        <form-item title="链接内容" :pass="true" value="game_url">
          <el-input class="imWidth100" type="textarea" :row="4" v-model="data.formParams.game_url" placeholder="请输入链接内容"
            clearable />
        </form-item>
        <form-item title="替换为" :pass="true" value="replace_url">
          <el-input class="imWidth100" type="textarea" :row="4" v-model="data.formParams.replace_url"
            placeholder="请输入替换内容" clearable />
        </form-item>
      </template>
    </BatchControl>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" @click="editBatchLink">确 定</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 批量编辑默认表 -->
  <el-dialog v-model="data.isShowBatch" title="批量编辑默认表链接" :close-on-click-modal="false" :close-on-press-escape="false"
    width="50%" top="25vh" @close="onHideDetails(false)">
    <PlatformGame ref="PlatformGameRef" @query="gameLink">
      <template #footer>
        <form-item title="链接内容" :pass="true" value="game_url">
          <el-input class="imWidth100" type="textarea" :row="4" v-model="data.formParams.game_url" placeholder="请输入链接内容"
            clearable />
        </form-item>
        <form-item title="替换为" :pass="true" value="replace_url">
          <el-input class="imWidth100" type="textarea" :row="4" v-model="data.formParams.replace_url"
            placeholder="请输入替换内容" clearable />
        </form-item>
      </template>
    </PlatformGame>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" @click="editBatchLink">确 定</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 游戏表初始化 -->
  <el-dialog v-model="data.isShowInitialize" :title="data.gameType == 1 ? '老虎机表初始化' : '游戏表初始化'"
    :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true" width="50%" top="25vh"
    @close="onHideDetails(false)">
    <form-item :title="data.gameType == 1 ? 't_slots_pggame的RTP字段' : 't_game表URL字段'" :pass="true" value="initialize">
      <el-select class="formItem320" v-model="data.initializeForm.initialize"
        :placeholder="data.gameType == 1 ? '请选择默认RTP字段' : '请选择URL字段'" filterable>
        <el-option v-for="(item, index) in data.gameType == 1 ? data.initializeTigerKey : data.initializeKey"
          :key="index" :label="'【' + item.value + '】' + item.label" :value="item.value" v-show="item.type" />
      </el-select>
    </form-item>
    <form-item :title="data.gameType == 1 ? 't_slots_pggame大倍数' : '同步表名c_game'" :pass="true" value="proportion">
      <el-select v-if="data.gameType == 1 ? true : false" class="formItem320" v-model="data.initializeForm.proportion"
        placeholder="请选择字段" filterable>
        <el-option label="a_slots_poolproportion_omg" value="a_slots_poolproportion_omg" />
        <el-option label="a_slots_poolproportion_demo" value="a_slots_poolproportion_demo" />
      </el-select>
      <span v-else>game_url</span>
    </form-item>
    <form-item title="t_slots_pggame倍数掌控" value="multiple" :pass="true" v-if="data.gameType == 1 ? true : false">
      <el-select class="formItem320" v-model="data.initializeForm.multiple" placeholder="请选择字段" filterable>
        <el-option label="a_slots_multiplecontrol_omg" value="a_slots_multiplecontrol_omg" />
        <el-option label="a_slots_multiplecontrol_demo" value="a_slots_multiplecontrol_demo" />
      </el-select>
    </form-item>
    <form-item title="其他字段" value="initialize">
      <el-select class="imWidth100" v-model="data.initializeForm.params" placeholder="请选择URL字段" filterable multiple
        @change="chooseInitial">
        <el-option label="全部" :value="-1" />
        <el-option v-for="(item, index) in data.gameType == 1 ? data.tigerKey : data.initializeKey" :key="index"
          :label="'【' + item.value + '】' + item.label" :value="item.value"
          v-show="!item.type && item.value != 'a_slots_rtp'" />
      </el-select>
    </form-item>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" @click="initializeGameTable">确 定</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 最小倍数押注初始化 -->
  <el-dialog v-model="data.isShowScale" title="最小倍数押注初始化" :close-on-click-modal="false" :close-on-press-escape="false"
    width="50%" top="25vh" @close="onHideDetails(false)">
    <BatchControl ref="BatchControlScaleRef" :gameType="data.gameType" :brandShow="true" />
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" @click="initializeScale">确 定</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 同步商户配置 -->
  <el-dialog v-model="data.isShowCommercial" title="同步商户配置" :close-on-click-modal="false" :close-on-press-escape="false"
    width="50%" :top="data.gameType == 1 ? '12vh' : '25vh'" @close="onHideDetails(false)">
    <div :class="data.gameType == 1 ? 'dialogMaxHeight' : ''">
      <BatchControl ref="BatchControlRef" :gameShow="data.gameType ? true : false" :gameType="data.gameType">
        <template #footer>
          <form-item title="全局表配置" :pass="true" value="ids" v-if="!data.gameType">
            <el-select class="imWidth100" v-model="data.commercialForm.ids" placeholder="请选择字段" filterable multiple
              @change="chooseGlobal">
              <el-option label="全部" :value="-1" />
              <el-option v-for="(item, index) in data.globalArray" :key="index"
                :label="'【' + item.id + '】' + item.remark" :value="item.id" />
            </el-select>
          </form-item>
          <form-item title="表格字段" :pass="true" value="params" v-if="data.gameType == 1 || data.gameType == -1">
            <span class="tipsTextRed" v-show="data.gameType == 1">* a_slots_backend_echo 字段尽量不要同步,每个商户需要RTP不一样</span>
            <el-select class="imWidth100" v-model="data.commercialForm.params" placeholder="请选择字段" filterable multiple
              @change="chooseKey">
              <el-option label="全部" :value="-1" />
              <el-option v-for="(item, index) in data.gameType == 1 ? data.tigerKey : data.gameKey" :key="index"
                :label="'【' + item.value + '】' + item.label" :value="item.value" />
            </el-select>
          </form-item>
        </template>
      </BatchControl>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" @click="initialSynchronization">确 定</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 千倍合约--同步商户配置 -->
  <el-dialog v-model="data.isShowThousandCommercial" title="千倍合约-同步商户配置" :close-on-click-modal="false"
    :close-on-press-escape="false" width="50%" :top="data.gameType == 1 ? '12vh' : '25vh'" :destroy-on-close="true"
    @close="onHideDetails(false)">
    <div :class="data.gameType == 1 ? 'dialogMaxHeight' : ''">
      <BatchControl ref="BatchControlThousandRef" :multipleShow="true" :gameShow="false">
        <template #footer>
          <form-item title="表格字段" :pass="true" value="params">
            <el-select class="imWidth100" v-model="data.thousandForm.params" placeholder="请选择字段" filterable multiple
              @change="chooseKey">
              <el-option label="全部" :value="-1" />
              <el-option v-for="(item, index) in data.thousandKey" :key="index"
                :label="'【' + item.value + '】' + item.label" :value="item.value" />
            </el-select>
          </form-item>
        </template>
      </BatchControl>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" @click="initialThousandSet">确 定</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 第三方游戏ID-批量平替开关 -->
  <el-dialog v-model="data.isShowThirdBatch" title="批量控制平替功能" :close-on-click-modal="false"
    :close-on-press-escape="false" width="50%" top="20vh" @close="onHideDetails(false)">
    <div class="dialogMaxHeight">
      <form-item title="游戏" :pass="true" value="game_ids">
        <el-select class="imWidth100" v-model="data.formParamsThirdBatch.game_ids" :disabled="data.thirdBatchData.length?false:true" placeholder="请选择" multiple filterable @change="chooseThird">
          <el-option label="全部" :value="-1"/>
          <el-option v-for="(item, index) in data.thirdBatchData" :key="index" :label="'【'+item.game_id+'】'+item.remark" :value="item.game_id" />
        </el-select>
      </form-item>
      <form-item title="平替功能开关" value="state">
        <el-radio-group v-model="data.formParamsThirdBatch.state" :disabled="data.formParamsThirdBatch.game_ids.length ? false : true"
          size="large">
          <el-radio-button label="关闭" :value="0" />
          <el-radio-button label="开启" :value="1" />
        </el-radio-group>
      </form-item>
    </div>
    <template #footer>
      <div class="dialog-footer" v-show="!data.isDisableUser">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" :disabled="data.formParamsThirdBatch.game_ids.length?false:true" @click="changeThirdBatch">确 定</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 第三方游戏ID -->
  <el-dialog v-model="data.isShowThirdDetails" title="第三方游戏ID" :close-on-click-modal="false"
    :close-on-press-escape="false" width="50%" top="16vh" @close="onHideDetails(false)">
    <div :class="data.isDisableUser ? 'disableUser' : ''">
      <el-form ref="ruleFormRef" :model="data.formParamsThird" :inline="true" :rules="rulesThird" :scroll-to-error="true">
        <form-item v-show="data.formParamsThird.id" title="游戏名称" :pass="true" value="remark">
        <GameHeaderInfo :game_id="data.formParamsThird.game_id" />
      </form-item>
      <form-item title="游戏备注" value="remark">
        <el-input v-model="data.formParamsThird.remark" placeholder="游戏备注" clearable />
        <span class="tipsText">(游戏名称填写)</span>
      </form-item>
      <form-item title="三方游戏我方定义ID" :pass="true" value="game_id">
        <el-input-number v-model="data.formParamsThird.game_id" :min="0" :max="99999999999999999999" :precision="0"
          :disabled="data.formParamsThird.id?true:false" placeholder="三方游戏我方定义ID" clearable controls-position="right" />
        <span class="tipsText">(三方的游戏我方接入后，我方定义的ID，此ID包括对外我方商户使用)</span>
      </form-item>
      <form-item title="三方实际游戏ID" :pass="true" value="third_game_id">
        <el-input v-model="data.formParamsThird.third_game_id" placeholder="三方实际游戏ID" clearable />
        <span class="tipsText">(三方的游戏三方定义的ID，此数据为三方接口返回)</span>
      </form-item>
      <form-item title="品牌" :pass="true" value="platform">
        <el-select v-model="data.formParamsThird.platform" placeholder="请选择" filterable>
          <el-option v-for="(item, index) in GameBrand" :key="index" :label="item.label" :value="item.value" />
        </el-select>
        <span class="tipsText">(选择此游戏的品牌)</span>
      </form-item>
      <form-item title="平替ID" value="alternate_id">
        <el-input-number v-model="data.formParamsThird.alternate_id" :min="0" :max="99999999999999999999" :precision="0"
          placeholder="三方游戏我方定义ID" clearable controls-position="right" />
          <span class="tipsText">(此处填写需平替游戏的ID，此游戏ID为我方自有研发的同款游戏)</span>
      </form-item>
      <form-item title="功能开关" value="state">
        <el-radio-group v-model="data.formParamsThird.state" :disabled="data.formParamsThird.alternate_id ? false : true"
          size="large">
          <el-radio-button label="关闭" :value="0" />
          <el-radio-button label="开启" :value="1" />
        </el-radio-group>
        <span class="tipsText">(开启功能后，平替功能生效，商户请求三方的游戏实际会进入到我方自有游戏内)</span>
      </form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer" v-show="!data.isDisableUser">
        <el-button type="info" @click="onHideDetails(false)">取 消</el-button>
        <el-button type="primary" @click="updateThird">确 定</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 第三方平台维护 -->
  <PlatformMaintenance :show="data.isShowThirdPlatform" @onHideDetails="onHideDetails" />
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, nextTick } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormRules } from 'element-plus'
import tableHeader from '@/components/tableHeader/tableHeader.vue'
import SearchTable from '@/components/SearchTable/SearchTable.vue'
import Operator, { type QueryType } from '@/components/FormItem/Operator.vue'
import GameItem, { type GameType } from '@/components/FormItem/GameItem.vue'
import GameHeaderInfo from '@/components/FormItem/GameHeaderInfo.vue'
import Game from '@/views/configManage/components/game.vue'
import Config from '@/views/configManage/components/config.vue'
import Global from '@/views/configManage/components/global.vue'
import BatchControl from '@/components/FormItem/BatchControl.vue'
import RtpControl from '@/components/FormItem/RtpControl.vue'
import PlatformMaintenance from '@/views/configManage/components/PlatformMaintenance.vue'
import PlatformGame, { type GameTypes } from '@/components/FormItem/PlatformGame.vue'
import { GAMETYPE, DEMOPLAY, GameBrand } from '@/config/eunm'
import { throttle, selectToLabel } from '@/utils/index'
import {
  getGame,
  getGlobal,
  coverGlobal,
  initializeGlobal,
  initializeSlot,
  initializeHundred,
  getOverloadTable,
  overloadTable,
  coverSlot,
  coverHundred,
  coverGame,
  editGameUrl,
  initializeGame,
  coverThousand,
  initializeThousand,
  getThirdGameId,
  addThirdGameId,
  editThirdGameId,
  editThirdBatchGameId,
  delThirdGameId,
  editMaintainGame,
  initializeBalanceScale,
  editAgentMaintainGame
} from '@/api/game'


import { useStore } from '@/stores/index'
const indexStore = useStore()

const data = reactive({
  tabDataList: [
    { label: '游戏表配置', value: -1, gameType: -1 },
    { label: '老虎机配置', value: 1, gameType: 1 },
    { label: '百人场配置', value: 2, gameType: 2 },
    { label: '全局表配置', value: 0, gameType: 0 },
    // { label: '商户功能表', value: 3, gameType: -1 },
    { label: '三方游戏功能', value: 4, gameType: -1 },
  ],
  query: {
    page: 1,
    size: 50,
    game_type: 1,
    agent_id: -1,
    operated_id: -1,
    platform: -1,
    game_id: -1,
    is_default_config: 1,
    is_alternate: 0,
  },
  list: [],
  total: 0,
  gameType: -1,
  fields: [] as any,
  formParamsThird: {
    id: 0,
    game_id: 0,
    remark: '',
    third_game_id: '',
    platform: '6',
    alternate_id: 0,
    state: 0,
  } as any,
  formParamsThirdBatch: {
    game_ids: [],
    state: 0,
  },
  globalQuery: {
    page: 1,
    size: 50,
    agent_id: 0,
    id: '',
    remark: '',
  },
  statusList: [
    { label: '-', value: 0 },
    { label: '开启', value: 1 },
    { label: '维护', value: 2 },
  ],
  dialogVisible: false,
  isShowDetails: false,
  isDisableUser: false,
  isShowThousandCommercial: false,
  isShowCommercial: false,
  isShowGame: false,
  isShowGlobal: false,
  isShowScale: false,
  isShowLoad: false,
  isShowControl: false,
  isShowBatch: false,
  isShowBatchAgent: false,
  isShowInitialize: false,
  isShowThirdBatch: false,
  isShowThirdDetails: false,
  isShowThirdPlatform: false,
  overloadTable: '',
  loadTable: [] as any[],
  commercialForm: {
    operated_id: -1,
    agent_ids: [],
    game_ids: [],
    ids: [],
    params: []
  },
  checkedItem: {} as any,
  checkedGameItem: {} as any,
  checkedGlobalItem: {} as any,
  tigerKey: [
    { label: '备注', value: 'remark' },
    { label: '行', value: 'a_slots_row' },
    { label: '列', value: 'a_slots_column' },
    { label: '止损止赢开关', value: 'a_slots_auto' },
    { label: '赢取展示', value: 'a_slots_winshow' },
    { label: '游戏用线', value: 'a_slots_lineuse' },
    { label: '游戏wild元素ID', value: 'a_slots_wildid' },
    { label: '线倍率', value: 'a_slots_linemagnification' },
    { label: 'slots押注', value: 'a_slots_amount' },
    { label: '默认押注', value: 'a_slots_defaultamount' },
    { label: '后台RTP显示', value: 'a_slots_rtpshow' },
    { label: 'RTP', value: 'a_slots_rtp' },
    { label: '小游戏图标', value: 'a_slots_scatterid' },
    { label: '赢取最高押注倍数', value: 'a_slots_maxwin' },
    { label: '购买小游戏功能', value: 'a_slots_buy' },
    { label: '关闭倍数(总押注超过最低押注的倍数关闭购买)', value: 'a_slots_multipleclose' },
    { label: '购买倍数(押注的倍数为购买花费)', value: 'a_slots_buymultiple' },
    { label: '游戏功能开启关闭', value: 'a_rtp_control' },
    { label: '多少次检测一次最新RTP', value: 'a_rtp_check' },
    { label: '控制配置:rtp范围,概率触发,使用RTP表', value: 'a_rtp_control_cfg' },
    { label: 'RTP在此范围不控制', value: 'a_rtp_setting' },
    { label: '当前rtp类型', value: 'a_slots_rtptype' },
    { label: '大倍数奖入池比例', value: 'a_slots_poolproportion', type: true },
    { label: '报奖倍数控制(档位,倍数下限,倍数上限)', value: 'a_slots_multiplecontrol', type: true },
    { label: '大倍数奖池控制(奖池编号,入池权重,爆池党委上线,爆池概率)', value: 'a_slots_bigamountcontrol' },
    { label: '奖池冷却', value: 'a_slots_cool' },
    { label: '购买超级免费游戏倍数', value: 'a_slots_buymultiple_two' },
    { label: '初始RTP后台回显', value: 'a_slots_backend_echo', type: false },
    { label: '小游戏元素ID多个配置', value: 'a_slots_scatter_ids' },
    { label: 'WILD元素ID多个配置', value: 'a_slots_wild_ids' },
    { label: '是否是pp热门游戏', value: 'a_pp_hot' },
  ],
  tigerKeyDiscrepancy: [
    { label: '游戏功能开启关闭', value: 'a_rtp_control', operator: [], agent: [], ids: [] },
    { label: '购买小游戏功能', value: 'a_slots_buy', operator: [], agent: [] },
    { label: '关闭倍数(总押注超过最低押注的倍数关闭购买)', value: 'a_slots_multipleclose', operator: [], agent: [], ids: [] },
    { label: 'slots押注', value: 'a_slots_amount', operator: [], agent: [], ids: [] },
    { label: '默认押注', value: 'a_slots_defaultamount', operator: [], agent: [], ids: [] },
  ],
  gameKey: [
    { label: 'GameUrl', value: 'game_url' },
    { label: '试玩路径', value: 'trial_path' },
    { label: '游戏中文名', value: 'game_name' },
    { label: '游戏英文名', value: 'game_name_en' },
    { label: '后台并入显示的品牌', value: 'platform_backend_echo' },
    { label: '是否是第三方游戏', value: 'game_source' },

  ],
  initializeTigerKey: [
    { label: 'a_slots_rtp_omg', value: 'a_slots_rtp_omg', type: true },
    { label: 'a_slots_rtp_gm', value: 'a_slots_rtp_gm', type: true },
  ],
  initializeKey: [
    { label: 'OMG本地服', value: 'omggame_url_local', type: true },
    { label: 'OMG测试服', value: 'omggame_url_test', type: true },
    { label: 'OMG正式服(美服)', value: 'omggame_url_us', type: true },
    { label: 'OMG正式服(亚服)', value: 'omggame_url_as', type: true },
    { label: 'OMG正式服(全球)', value: 'game_url_world', type: true },
    { label: 'GM(正式服）全球', value: 'game_url_worldas', type: true },
    { label: 'GM测试服', value: 'gmgame_url_test', type: true },
    { label: 'GM正式服(美服)', value: 'gmgame_url_us', type: true },
    { label: 'GM正式服(亚服)', value: 'gmgame_url_as', type: true },
    { label: 'GM和OMG演示服', value: 'game_url_demo', type: true },
    { label: 'game_id', value: 'game_id' },
    { label: 'game_name', value: 'game_name' },
    { label: 'game_name_en', value: 'game_name_en' },
    { label: 'icon_url', value: 'icon_url' },
    { label: '1:spribe,2:PG,3:JILI,4:PP,5:OMG', value: 'platform' },
    { label: 'game_type', value: 'game_type' },
    { label: 'show', value: 'show' },
    { label: 'trial_path', value: 'trial_path' },
    { label: 'status', value: 'status' },
    { label: 'test_account', value: 'test_account' },
    { label: '后台并入显示的品牌', value: 'platform_backend_echo' },
    { label: '是否是第三方游戏', value: 'game_source' },
  ],
  initializeForm: {
    initialize: '',
    proportion: '',
    multiple: '',
    params: [-1],
  },
  thousandKey: [
    { label: '备注', value: 'remark' },
    { label: '币种开关', value: 'a_crypto_control' },
    { label: '简称', value: 'a_crypto_name' },
    { label: '币种ID', value: 'a_crypto_instrument_id' },
    { label: '此币种在游戏内显示的小数位数', value: 'a_crypto_precision' },
    { label: '爆仓公式因数', value: 'a_crypto_bust_buffer' },
    { label: '最低下注额度', value: 'a_crypto_min_user_position' },
    { label: '用户最大持仓量', value: 'a_crypto_max_user_position' },
    { label: '最大亏损切割持仓量', value: 'a_crypto_max_pnl_cut_position' },
    { label: '利率(资金费率)', value: 'a_crypto_interest_rate' },
    { label: '费率起收小时数', value: 'a_crypto_interest_hours' },
    { label: '起收门槛', value: 'a_crypto_interest_threshold' },
    { label: '起始基础利率', value: 'a_crypto_base_rate' },
    { label: '费率倍数', value: 'a_crypto_rate_multiplier' },
    { label: '费率指数', value: 'a_crypto_rate_exponent' },
    { label: '资金持仓费率', value: 'a_crypto_funding_fate' },
    { label: '持仓规模与实际投资金额的比例', value: 'a_crypto_position_multiplier' },
    { label: '固定成本费用', value: 'a_crypto_fixed_cost' },
    { label: '市场影响成本', value: 'a_crypto_impact_cost' },
    { label: '市场反应速度', value: 'a_crypto_impact_speed' },
    { label: '图标ID', value: 'a_crypto_icon_id' },
    { label: '颜色代码', value: 'a_crypto_ui_color' },
    { label: '资产的排序顺序', value: 'a_crypto_sort_order' },
    { label: '交易所对应ID', value: 'a_crypto_okx_inst_id' },
    { label: '服务器所需字段', value: 'a_crypto_binance_symbol' },
    { label: '持仓时间小时上限', value: 'a_crypto_holding_time' },
  ],
  thousandForm: {
    agent_ids: [],
    ids: [],
    params: []
  },
  formParams: {
    platform: -1,
    game_ids: [],
    agent_ids: [],
    game_url: '',
    replace_url: '',
  },
  controlList: [] as any[],
  whiteList: [] as any[],
  rtpList: [] as any[],
  globalArray: [] as any[],
  thirdBatchData: [] as any[],
})

// 商户类别 + 商户
const queryItem = (item: QueryType) => {
  data.query.agent_id = item.agent_id
  data.query.operated_id = item.operated_id
}
// 游戏品牌 + 游戏
const queryGame = (item: GameType) => {
  data.query.platform = item.platform
  data.query.game_id = item.game_id
}

onMounted(async () => {
  GameItemRef.value.getGameType(-1)
  nextTick(() => {
    handleClick()
    getTableList()
  })
})

// 查询数据
const getSearchTypeList = async () => {
  data.list = []
  data.total = 0
  indexStore.setPageLoading(true)
  if (data.gameType) {
    if (data.gameType == 4) {
      getThirdGameIdList()
    } else {
      getSearchList()
    }
  } else {
    getGlobalList()
  }
}
const getSearchList = async () => {
  let query = JSON.parse(JSON.stringify(data.query));
  if (query.platform == -1) {
    query.platform = ''
  }
  let res = await getGame(query)
  data.list = res.data.list
  data.total = res.data.total
}
const getGlobalList = async () => {
  let query = JSON.parse(JSON.stringify(data.globalQuery))
  query.id = Number(query.id)
  let res = await getGlobal(query)
  data.list = res.data.list
  data.total = res.data.total
}
const getThirdGameIdList = async (show?:boolean) => {
  if(show){
    data.query.is_alternate = 1 
  }else{
    data.query.is_alternate = 0 
  }
  let query = JSON.parse(JSON.stringify(data.query));
  if (query.platform == -1) {
    query.platform = ''
  }
  let res = await getThirdGameId(query)
  res.data.list.forEach((item: any, index: number) => {
    item.id = index + 1
  })
  if(show){
    data.thirdBatchData = res.data.list
    data.isShowThirdBatch = true
  }else{
    data.list = res.data.list
    data.total = res.data.total
  }
}
// 重载表
const getTableList = async () => {
  let res = await getOverloadTable({})
  data.loadTable = res.data
}
// 选择游戏tab
const GameItemRef = ref<any>()
const SearchTableRef = ref<any>()
const handleClick = async () => {
  // 游戏表配置
  if (data.gameType == -1) {
    data.fields = [
      { label: '游戏ID', prop: 'game_id', width: '120', visible: true, tips: '游戏的ID' },
      { label: '游戏名称', prop: 'game_name', width: '200', visible: true, tips: '游戏名称的描述说明' },
      { label: '游戏英文名称', prop: 'game_name_en', width: '220', visible: true, tips: '游戏名称的英文描述说明' },
      { label: '游戏类型', prop: 'game_type', width: '120', visible: true, tips: '此游戏的类型，区分为百人、MiniGame 、老虎机' },
      { label: '游戏状态', prop: 'status', width: '140', visible: true, tips: '此游戏的状态开启才可以进入，维护关闭' },
      { label: 'GameUrl', prop: 'game_url', width: '', visible: true, tips: '此游戏的GameUrl路径' },
    ]
  }
  // 老虎机 百人场
  if (data.gameType == 1 || data.gameType == 2) {
    data.fields = [
      { label: '游戏ID', prop: 'game_id', width: '180', visible: true, tips: '游戏的ID' },
      { label: '游戏类型', prop: 'game_type', width: '150', visible: true, tips: '此游戏的类型，区分为百人、MiniGame 、老虎机' },
      { label: '游戏名称', prop: 'game_name', width: '', visible: true, tips: '游戏名称的描述说明' },
      { label: '游戏英文名称', prop: 'game_name_en', width: '220', visible: true, tips: '游戏名称的英文描述说明' },
    ]
  }
  // 全局表
  if (!data.gameType) {
    data.fields = [
      { label: 'ID', prop: 'id', width: '90', visible: true, tips: '此数据的ID' },
      { label: '备注', prop: 'remark', width: '380', visible: true, tips: '此数据的备注' },
      { label: '参数', prop: 'a_global_parameter', width: '', visible: true, tips: '此数据的参数' },
    ]
  }
  // 商户功能表
  if (data.gameType == 3) {
    data.fields = [
      { label: '上级渠道', prop: 'operator_name', width: '150', visible: true, tips: '商户的上级代理' },
      { label: '商户名称', prop: 'agent_id', width: '150', visible: true, tips: '商户的名称' },
    ]
  }
  //三方游戏功能
  if (data.gameType == 4) {
    data.fields = [
      { label: '游戏ID', prop: 'game_id', width: '160', visible: true, tips: '游戏的ID' },
      { label: '游戏名称', prop: 'game_name', width: '240', visible: true, tips: '游戏名称的描述说明' },
      { label: '三方游戏实际ID', prop: 'third_game_id', width: '160', visible: true, tips: '三方的游戏三方定义的ID' },
      { label: '三方游戏实际品牌', prop: 'platform', width: '180', visible: true, tips: '游戏对应的品牌' },
      { label: '平替ID', prop: 'alternate_id', width: '160', visible: true, tips: '此游戏ID为我方自有研发的同款游戏' },
      { label: '功能状态', prop: 'state', width: '120', visible: true, tips: '是否开启平替功能，开启后后端会将三方游戏指向至我方对应的游戏' },
      { label: '游戏备注', prop: 'remark', width: '', visible: true, tips: '游戏名称的备注' },
      { label: '游戏状态', prop: 'status', width: '120', visible: true, tips: '此游戏的状态开启才可以进入，维护关闭' },
    ]
  }
  SearchTableRef.value.initTableMaxHeight()
  resetDetails()
}

// 初始化配置 老虎机1 百人场2 捕鱼3 街机4 
const initialConfiguration = throttle(async () => {
  if (data.gameType == 1) {
    return data.isShowInitialize = true
  } else {
    let message = '是否初始化默认配置？';
    ElMessageBox.confirm(message, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    }).then(async () => {
      if (data.gameType == 2) {
        await initializeHundred({})
      } else {
        await initializeGlobal({})
      }
      ElMessage.success('初始化成功')
      onHideDetails(true)
    }).catch(() => { })
  }
}, 5000)
// 初始化-千倍合约
const initializeThousandSet = throttle(async () => {
  ElMessageBox.confirm('是否初始化千倍合约？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    await initializeThousand({})
    ElMessage.success('初始化成功')
  }).catch(() => { })
}, 5000)
// 初始化游戏表
const initializeGameTable = throttle(async () => {
  let query = JSON.parse(JSON.stringify(data.initializeForm));
  if (!query.initialize) {
    return ElMessage.error('请选择至少选择一个表字段同步')
  }
  const all = data.initializeForm.params.find((item: number) => item == -1)
  if (all) {
    query.params = []
    if (data.gameType == 1) {
      data.tigerKey.forEach((item: any) => {
        if (item.value != 'a_slots_rtp' && !item.type) {
          query.params.push(item.value)
        }
      })
    } else {
      data.initializeKey.forEach((item: any) => {
        if (!item.type) {
          query.params.push(item.value)
        }
      })
    }
  }
  query.params.push(query.initialize)
  if (data.gameType == 1) {
    if (!data.initializeForm.proportion) {
      return ElMessage.error('请选择t_slots_pggame大倍数')
    }
    if (!data.initializeForm.multiple) {
      return ElMessage.error('请选择t_slots_pggame倍数掌控')
    }
    query.params.push(data.initializeForm.proportion, data.initializeForm.multiple)
  }
  data.gameType == 1 ? await initializeSlot(query) : await initializeGame(query)
  indexStore.getGame() //游戏汇总
  ElMessage.success('初始化成功')
  onHideDetails(true)
}, 5000)
// 选则初始化表字段
const chooseInitial = async (row: any) => {
  const all = row.find((item: number) => item == -1)
  if (all) {
    data.initializeForm.params = [-1] as any
  } else {
    data.initializeForm.params = row
  }
}
// 客户端最小倍数押注初始化
const initializeScale = async () => {
  const parameter = {} as any;
  Object.assign(parameter, BatchControlScaleRef.value.SetBatchControl())
  if (parameter.agent_ids && !parameter.agent_ids.length) {
    return false
  }
  if (parameter.game_ids && !parameter.game_ids.length) {
    return false
  }
  ElMessageBox.confirm('是否初始化选择的商户游戏最小倍数押注？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    await initializeBalanceScale(parameter)
    ElMessage.success('初始化成功')
    onHideDetails(false)
  }).catch(() => { })
}

// 详情
const checkDetails = (row: any, type: string) => {
  if (type == 'check') {
    data.isDisableUser = true
  } else {
    data.isDisableUser = false
  }
  if (data.gameType) {
    if (data.gameType == -1) {
      data.isShowGame = true
      data.checkedGameItem = JSON.parse(JSON.stringify(row))
    } else if (data.gameType == 4) {
      data.isShowThirdDetails = true
      data.formParamsThird = JSON.parse(JSON.stringify(row))
    } else {
      data.isShowDetails = true
      Object.assign(data.checkedItem, row)
    }
  } else {
    data.checkedGlobalItem = JSON.parse(JSON.stringify(row))
    data.isShowGlobal = true
  }
}
// 第三方平替游戏ID
const chooseThird = (row:any) => {
  const all = row.find((item: number) => item == -1)
  if (all) {
    data.formParamsThirdBatch.game_ids = [-1] as any
  } else {
    data.formParamsThirdBatch.game_ids = row
  }
}
// 批量修改第三方平替游戏ID
const changeThirdBatch = throttle( async () => {
  let query = JSON.parse(JSON.stringify(data.formParamsThirdBatch))
  query.alternate_ids = [] as any[]
  const all = query.game_ids.find((item: number) => item == -1)
  if (all) {
    query.game_ids = [] as any[]
    data.thirdBatchData.forEach((item:any)=>{
      query.game_ids.push(item.game_id)
    })
  }
  data.thirdBatchData.forEach((item:any)=>{
    query.game_ids.forEach((second:any)=>{
      if(item.game_id == second){
        query.alternate_ids.push(item.alternate_id)
      }
    })
  })
  await editThirdBatchGameId(query)
  ElMessage.success('操作成功')
  data.formParamsThirdBatch = {
    game_ids: [],
    state: 0,
  }
  handleClick()
},5000)
const rulesThird = reactive<FormRules<typeof data.formParamsThird>>({
  game_id: [{ required: true, message: '请输入三方游戏我方定义ID', trigger: 'blur' }],
  third_game_id: [{ required: true, message: '请输入三方实际游戏ID', trigger: 'blur' }]
})
// 新增/修改第三方ID
const updateThird = throttle(async () => {
  const parameter = JSON.parse(JSON.stringify(data.formParamsThird))
  ElMessageBox.confirm(parameter.id ? `是否平替游戏【${parameter.game_id}】为【${parameter.alternate_id}】?` : `是否新增平替游戏【${parameter.game_id}】为【${parameter.alternate_id}】??`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    parameter.id ? await editThirdGameId(parameter) : await addThirdGameId(parameter)
    ElMessage.success('操作成功')
    data.isShowThirdDetails = false
    data.formParamsThird = {
      id: 0,
      game_id: 0,
      remark: '',
      third_game_id: 0,
      platform: '6',
      alternate_id: 0,
      state: 0,
    }
    getThirdGameIdList()
  }).catch(() => { })
},5000)
// 删除第三方ID
const deleteThird = (row:any) => {
  ElMessageBox.confirm(`确认是否删除【${row.remark}】游戏？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    await delThirdGameId({
      game_id: row.game_id,
    })
    ElMessage.success('操作成功')
    getSearchTypeList()
  }).catch(() => { })
}
// 维护第三方平替ID
const maintenanceGame = (row:any,type:boolean) => {
  let dataRow = JSON.parse(JSON.stringify(row));
  dataRow.agent_ids = []
  indexStore.agentArray.forEach((item:any)=>{
    dataRow.agent_ids.push(item.value)
  })
  ElMessageBox.confirm(`确认是否${type?'开启':'维护'}所有商户的【${row.remark}】游戏？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    await editMaintainGame({
      game_ids: [row.game_id,row.alternate_id],
      agent_ids: dataRow.agent_ids,
      status: type ? 1:0,
      is_all: 1,
    })
    if(dataRow.platform == '6'){
      await editAgentMaintainGame({
        gamelist: [row.game_id,row.alternate_id],
        maintain: type ? []:dataRow.agent_ids,
        running: type ? dataRow.agent_ids:[],
      })
    }
    ElMessage.success('操作成功')
    getSearchTypeList()
  }).catch(() => { })
}
// 批量编辑链接
const gameLink = (item: GameTypes) => {
  data.formParams.platform = item.platform
  data.formParams.game_ids = item.game_ids
}
const gameLinkAgent = (item: any) => {
  data.formParams.platform = item.platform
  data.formParams.game_ids = item.game_ids
  data.formParams.agent_ids = item.agent_ids
}
const editBatchLink = throttle(async () => {
  let query = JSON.parse(JSON.stringify(data.formParams));
  if (!query.game_ids.length) {
    return ElMessage.error('请选择替换游戏')
  }
  if (!query.game_url || !query.replace_url) {
    return ElMessage.error('请输入链接内容')
  }
  if (!query.agent_ids.length) {
    query.agent_ids = [0]
  }
  const all = query.game_ids.find((item: number) => item == -1)
  if (all) {
    query.game_ids = []
    indexStore.gameArray.forEach((item: any) => {
      query.game_ids.push(item.value)
    })
  }
  await editGameUrl(query)
  ElMessage.success('编辑链接成功')
  onHideDetails(true)
},5000)

// 重载表
const overload = async () => {
  if (data.overloadTable) {
    let name = ''
    data.loadTable.forEach((item: any) => {
      if (data.overloadTable == item.table_name) {
        name = item.table_comment
      }
    })
    ElMessageBox.confirm(`是否重载【${data.overloadTable}】${name}`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    }).then(async () => {
      await overloadTable({ table_name: data.overloadTable })
      ElMessage.success('重载成功')
      onHideDetails(false)
    }).catch(() => { })
  }
}

// 新增默认配置
const addDefaultConfig = async () => {
  if (data.gameType) {
    data.isShowDetails = true
  } else {
    data.isShowGlobal = true
  }
}
// 调控配置
const controlDetails = async (row: any) => {
  data.isShowControl = true
  data.checkedItem = row
}

// 选择总代-查询总代下所有商户
const getCommercialList = async (id: number) => {
  data.commercialForm.agent_ids = []
  data.commercialForm.game_ids = []
  data.commercialForm.ids = []
  if (!data.gameType) {
    getAllGlobalList()
  }
}
// 字段
const chooseKey = async (row: any) => {
  const all = row.find((item: number) => item == -1)
  if (all) {
    data.commercialForm.params = [-1] as any
  } else {
    data.commercialForm.params = row
  }
}
const chooseGlobal = async (row: any) => {
  const all = row.find((item: number) => item == -1)
  if (all) {
    data.commercialForm.ids = [-1] as any
  } else {
    data.commercialForm.ids = row
  }
}
const getAllGlobalList = async () => {
  let res = await getGlobal({
    page: 1,
    size: 9999
  })
  data.globalArray = res.data.list
}

// 商户同步配置
const initialSynchronization = throttle(async () => {
  const parameter = JSON.parse(JSON.stringify(data.commercialForm))
  let message = '是否将配置同步到选择的商户？';
  const allKey = data.commercialForm.params.find((item: number) => item == -1)
  const allGlobal = data.commercialForm.ids.find((item: number) => item == -1)
  if (allKey) {
    parameter.params = []
    if (data.gameType == 1) {
      data.tigerKey.forEach((item: any) => {
        parameter.params.push(item.value)
      })
    } else {
      data.gameKey.forEach((item: any) => {
        parameter.params.push(item.value)
      })
    }
  }
  if (allGlobal) {
    parameter.ids = []
    data.globalArray.forEach((item: any) => {
      parameter.ids.push(item.id)
    })
  }
  Object.assign(parameter, BatchControlRef.value.SetBatchControl())
  if (parameter.close) {
    return false
  }
  if (!parameter.params.length && data.gameType == 1) {
    return ElMessage.error('请选择表格字段')
  }
  ElMessageBox.confirm(message, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    switch (data.gameType) {
      case -1:
        await coverGame(parameter)
        break;
      case 0:
        await coverGlobal(parameter)
        break;
      case 1:
        await coverSlot(parameter)
        break;
      case 2:
        await coverHundred(parameter)
        break;
    }
    ElMessage.success('同步成功')
    onHideDetails(true)
  }).catch(() => { })
}, 5000)
// 同步商户-千倍合约
const initialThousandSet = throttle(async () => {
  const parameter = JSON.parse(JSON.stringify(data.thousandForm))
  let message = '是否将配置同步到选择的商户？';
  const all = data.thousandForm.agent_ids.find((item: number) => item == -1)
  const allKey = data.thousandForm.params.find((item: number) => item == -1)
  if (all) {
    parameter.ids = []
    indexStore.agentArray.forEach((item: any) => {
      parameter.ids.push(item.id)
    })
  }
  if (allKey) {
    parameter.params = []
    data.thousandKey.forEach((item: any) => {
      parameter.params.push(item.value)
    })
  }
  Object.assign(parameter, BatchControlThousandRef.value.SetBatchControl())
  ElMessageBox.confirm(message, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    await coverThousand(parameter)
    ElMessage.success('同步成功')
    onHideDetails(true)
  }).catch(() => { })
}, 5000)

// 修改页数
const changeSize = (val: number) => {
  if (data.gameType) {
    data.query.size = val
  } else {
    data.globalQuery.size = val
  }
}
const changePage = (val: number) => {
  if (data.gameType) {
    data.query.page = val
  } else {
    data.globalQuery.page = val
  }
  getSearchTypeList()
}
// 重置表头
const resetData = (val: any) => {
  data.fields = val
}
// 重置
const BatchControlRef = ref<any>()
const PlatformGameRef = ref<any>()
const OperatorRef = ref<any>()
const BatchControlAgentRef = ref<any>()
const BatchControlThousandRef = ref<any>()
const BatchControlScaleRef = ref<any>()
const onHideDetails = (show: boolean) => {
  data.isShowDetails = false
  data.isDisableUser = false
  data.dialogVisible = false
  data.isShowGame = false
  data.isShowGlobal = false
  data.isShowLoad = false
  data.isShowThirdDetails = false
  data.isShowThirdPlatform = false
  data.isShowInitialize = false
  data.isShowControl = false
  data.isShowThirdBatch = false
  // 批量编辑默认表链接
  if (data.isShowBatch) {
    PlatformGameRef.value.onHideDetails(-1)
  }
  data.isShowBatch = false
  // 批量编辑商户链接
  if (data.isShowBatchAgent) {
    BatchControlAgentRef.value.onHideDetails()
  }
  data.isShowBatchAgent = false
  // 同步商户配置
  if (data.isShowCommercial) {
    BatchControlRef.value.onHideDetails()
  }
  data.isShowCommercial = false
  // 同步商户千倍合约
  if (data.isShowThousandCommercial) {
    BatchControlThousandRef.value.onHideDetails()
  }
  data.isShowThousandCommercial = false
  // 最小压注倍数初始化
  if (data.isShowScale) {
    BatchControlScaleRef.value.onHideDetails()
  }
  data.isShowScale = false

  data.overloadTable = ''
  data.formParams = {
    platform: -1,
    game_ids: [],
    agent_ids: [],
    game_url: '',
    replace_url: '',
  }
  data.formParamsThird = {
    id: 0,
    game_id: 0,
    remark: '',
    third_game_id: '',
    platform: '6',
    alternate_id: 0,
    state: 0,
  }
  data.initializeForm = {
    initialize: '',
    proportion: '',
    multiple: '',
    params: [-1],
  }
  data.commercialForm = {
    operated_id: -1,
    agent_ids: [],
    game_ids: [],
    ids: [],
    params: []
  }
  data.thousandForm = {
    agent_ids: [],
    ids: [],
    params: []
  },
  data.formParamsThirdBatch = {
    game_ids: [],
    state: 0,
  }
  data.checkedGameItem = {} as any
  data.checkedGlobalItem = {} as any
  if (show) {
    getSearchTypeList()
  }
}
const resetDetails = () => {
  data.query = {
    page: 1,
    size: 50,
    game_type: selectToLabel(data.tabDataList, data.gameType, 'gameType', 'value'),
    agent_id: -1,
    operated_id: -1,
    platform: -1,
    game_id: -1,
    is_default_config: 1,
    is_alternate: 0,
  },
  data.globalQuery = {
    page: 1,
    size: 50,
    agent_id: 0,
    id: '',
    remark: '',
  }
  OperatorRef.value.onHideDetails()
  GameItemRef.value.onHideDetails(selectToLabel(data.tabDataList, data.gameType, 'gameType', 'value'))
  getSearchTypeList()
}
</script>

<style scoped lang="scss">
.paddingLeft42 {
  padding-left: 42%;
}

.transparency {
  display: none;
}
</style>
